using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient ;
using System.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ACSeguridad
{
   public class DAEnterpriseValidate
   {
        #region Variables

       DbCommand  pdbComando;

        DbParameter pdbParametro;
        SqlDatabase psqlDatabase;
        ConnectionString csConeccion;
        //'Private m_db As Database
       

        String pstrServer;
        String pstrDatabase;
        String pstrUser;
        String pstrPwd;
       String pstrConeccion;
        DbTransaction m_tran = null;
        String m_sp;
        #endregion

        #region Constructores
           
       public DAEnterpriseValidate(String xstrServer, String xstrDatabase, String xstrUsuario, String xstrClave)
       {
            pstrDatabase = xstrDatabase;
            pstrServer = xstrServer;
            pstrUser = xstrUsuario;
            pstrPwd = xstrClave;
            pstrConeccion = GetConeccion();
            
            psqlDatabase = new SqlDatabase(pstrConeccion);
       }
       private String GetConeccion()
       {
           return "Database=" + pstrDatabase + ";Server=" + pstrServer + ";User ID=" + pstrUser + ";Password=" + pstrPwd + ";";
       }

        #endregion

       #region Propiedades
       public string SPNombre
       {
           set
           { 
               pdbComando = psqlDatabase.GetStoredProcCommand(value);
                pdbComando.CommandTimeout = 60;
           }
       }
       public DbCommand DBComando
       {
           get { return pdbComando; }
           set { pdbComando = value; }
       }

       #endregion

       #region Metodos
       public void AsignarProcedure(String x_sp, CommandType x_tipo)
       {
           if (x_tipo == CommandType.Text)
           {
               pdbComando = psqlDatabase.GetSqlStringCommand(x_sp);
           }
           else if (x_tipo == CommandType.StoredProcedure)
           {
               pdbComando = psqlDatabase.GetStoredProcCommand(x_sp);
           }
       }
       public void AgregarParametro(String xstrNombre, System.Object xvarValor)
       {
           pdbParametro = pdbComando.CreateParameter();
            pdbParametro.ParameterName = xstrNombre;
            pdbParametro.Value = xvarValor;
            pdbParametro.Direction = ParameterDirection.Input;

            pdbComando.Parameters.Add(pdbParametro);
       }
       public DbParameter AgregarParametro(String paramNombre, Object paramValor , DbType paramTipo , int paramLongitud, ParameterDirection paramDireccion )
       {
           try
           {
                pdbParametro = pdbComando.CreateParameter();
                pdbParametro.ParameterName = paramNombre;

                if (paramValor == null)
                {
                    pdbParametro.Value = System.DBNull.Value;
                }
                else { pdbParametro.Value = paramValor; }

                pdbParametro.Size = paramLongitud;
                pdbParametro.DbType = paramTipo;
                pdbParametro.Direction = paramDireccion;
                pdbComando.Parameters.Add(pdbParametro);
                return pdbParametro;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
       public DataSet ExecuteMultiRows(string xstrNombreTabla)
       {
           DataSet ldstDatos = new DataSet();
           try 
            {	        
	             ldstDatos = psqlDatabase.ExecuteDataSet(pdbComando);
            }
            catch (Exception ex)
            {
	            throw ex;
            }
           ldstDatos.Tables[0].TableName = xstrNombreTabla;
           return ldstDatos;
       }
       public int ExecuteNonQuery()
       {
           int l_filas = 0;
            try 
	        {	        
        	    l_filas = psqlDatabase.ExecuteNonQuery(pdbComando);
                return l_filas;
	        }
	        catch (Exception ex)
	        {
		        throw ex;
	        }
       }
       public DataSet ExecuteDataSet()
       {
           DataSet l_datos = new DataSet();
           try
           {
               l_datos = psqlDatabase.ExecuteDataSet(pdbComando);
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return l_datos;
       }


       #endregion
   }
}
